﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AdvancedDirectXMeshConverter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void BrowseInput_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                InputFile.Text = openFileDialog1.FileName;
                Output_CheckedChanged(OutSDKMesh, EventArgs.Empty);
            }
        }

        private void BrowseOutput_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                OutputFile.Text = openFileDialog1.FileName;
            }
        }

        private void Convert_Click(object sender, EventArgs e)
        {
            string arguments = "";

            if (!File.Exists(InputFile.Text))
                return;

            arguments += InputFile.Text + " ";
            arguments += (generateNormals.Checked ? "-n " : "");
            arguments += (tangent1.Checked ? "-t " : "-tb ");
            arguments += (OutSDKMesh.Checked ? "-sdkmesh " : (OutX.Checked ? "-xt " : "-x "));
            arguments += (OverrideExistingFile.Checked ? "-y " : "");
            arguments += "-o " + OutputFile.Text + " ";

            if (!File.Exists("MeshConvert.exe"))
            {
                MessageBox.Show("MeshConvert.exe not found.", "File Not Found", MessageBoxButtons.OK);
                return;
            }
            Process meshConvert = new Process();
            meshConvert.StartInfo.FileName = "MeshConvert.exe";
            meshConvert.StartInfo.Arguments = arguments;
            if(!meshConvert.Start())
            {
                MessageBox.Show("Error Start MeshConvert.exe.", "Error", MessageBoxButtons.OK);
                return;
            }
        }

        private void Output_CheckedChanged(object sender, EventArgs e)
        {
            if (!File.Exists(InputFile.Text))
                return;

            FileInfo inputFileInfo = new FileInfo(InputFile.Text);
            OutputFile.Text = inputFileInfo.DirectoryName + "\\" + Path.GetFileNameWithoutExtension(inputFileInfo.Name) + (OutSDKMesh.Checked ? ".sdkmesh" : ".x");

            if(InputFile.Text == OutputFile.Text)
            {
                OutputFile.Text = inputFileInfo.DirectoryName + "\\" + Path.GetFileNameWithoutExtension(inputFileInfo.Name) + "_copy" + (OutSDKMesh.Checked ? ".sdkmesh" : ".x");
            }
        }
    }
}
